package org.lazy.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.lazy.util.StringUtil;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * 
 * @description XML解析工具类
 * @author Anan
 * @time 2013-9-23 下午2:42:07
 */
public class XmlUtil {
	private final static String ATTR_ID = "id";
	private final static String ATTR_NAME = "name";

	private XmlUtil() {
	};

	/**
	 * 
	 * @description 加载文档
	 * @author Anan
	 * @time 2013-9-23 下午1:36:00
	 * @param xmlFilePath
	 *            xml文件路径
	 * @return
	 */
	public static Document getDocument(String xmlFilePath) {
		Document doc = null;
		FileInputStream fis = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			fis = new FileInputStream(xmlFilePath);
			builder = factory.newDocumentBuilder();
			doc = builder.parse(fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return doc;
	}

	/**
	 * 
	 * @description 获取根节点
	 * @author Anan
	 * @time 2013-9-23 下午1:36:24
	 * @param doc
	 * @return
	 */
	public static Node getRoot(Document doc) {
		return doc.getDocumentElement();
	}
	/**
	 * 
	 * @description 根据节点属性ID获取节点
	 * @author Anan
	 * @time  2013-9-23 下午3:17:29
	 * @param id   属性id
	 * @param parentNode  父节点
	 * @return
	 */
	public static Node getChildById(String id, Node parentNode) {
		Node result = null;
		NodeList nodeList = parentNode.getChildNodes();
		if (nodeList != null) {
			int nodeListSize = nodeList.getLength();
			for (int i = 0; i < nodeListSize; i++) {
				Node n = nodeList.item(i);
				if (n.getNodeType() == Node.ELEMENT_NODE) {
					if (id.equals(getNodeAttr(ATTR_ID,n))) {
						result = n;
					}
				}
			}
		}
		return result;
	}
	/**
	 * 
	 * @description 在parentNode节点下，获取固定属性name的节点
	 * @author Anan
	 * @time  2013-9-23 下午3:18:25
	 * @param name  属性名
	 * @param parentNode 父节点
	 * @return
	 */
	public static List<Node> getChildByName(String name,Node parentNode){
		List<Node> result = new ArrayList<Node>();
		if(StringUtil.isBlank(name)){return result;}
		NodeList nodeList = parentNode.getChildNodes();
		if (nodeList != null) {
			int nodeListSize = nodeList.getLength();
			for (int i = 0; i < nodeListSize; i++) {
				Node n = nodeList.item(i);
				if (n.getNodeType() == Node.ELEMENT_NODE) {
					if (name.equals(getNodeAttr(ATTR_NAME,n))) {
						result.add(n);
					}
				}
			}
		}
		return result;
	}
	/**
	 * 
	 * @description 在parentNode节点下，获取固定节点名的节点
	 * @author Anan
	 * @time  2013-9-23 下午3:18:25
	 * @param nodeName  节点名
	 * @param parentNode 父节点
	 * @return
	 */
	public static List<Node> getChildByNodeName(String nodeName,Node parentNode){
		List<Node> result = new ArrayList<Node>();
		if(StringUtil.isBlank(nodeName)){return result;}
		NodeList nodeList = parentNode.getChildNodes();
		if (nodeList != null) {
			int nodeListSize = nodeList.getLength();
			for (int i = 0; i < nodeListSize; i++) {
				Node n = nodeList.item(i);
				if (n.getNodeType() == Node.ELEMENT_NODE && nodeName.equals(n.getNodeName())) {
					result.add(n);
				}
			}
		}
		return result;
	}
	
	/**
	 * 
	 * @description 获取节点的属性值
	 * @author Anan
	 * @time  2013-9-23 下午2:40:02
	 * @param node  节点
	 * @param attrKey 属性名
	 * @return
	 */
	public static String getNodeAttr(String attrKey,Node node) {
		String attrVal = null;
		if (node == null) {
			return null;
		}
		NamedNodeMap namedNodeMap = node.getAttributes();
		if (namedNodeMap == null) {
			return null;
		}
		Node attr = namedNodeMap.getNamedItem(attrKey);
		if (attr == null) {
			return null;
		}
		attrVal = attr.getNodeValue();
		return attrVal;
	}
}
